package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.WeiXinUserUtils;
import com.corechina.apex_osswords.domain.dao.CoreEquipmentCheckResultMapper;
import com.corechina.apex_osswords.domain.dao.CorePollingEquipmentMapper;
import com.corechina.apex_osswords.domain.dao.CorePollingPlanMapper;
import com.corechina.apex_osswords.domain.dto.CoreEquipmentCheckResultDto;
import com.corechina.apex_osswords.domain.entity.CoreEquipmentCheckResult;
import com.corechina.apex_osswords.domain.entity.CorePollingEquipment;
import com.corechina.apex_osswords.service.ICoreEquipmentCheckResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 巡查设备结果表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-03-09
 */
@Service
public class CoreEquipmentCheckResultServiceImpl extends ServiceImpl<CoreEquipmentCheckResultMapper, CoreEquipmentCheckResult> implements ICoreEquipmentCheckResultService {

    @Autowired
    private CorePollingPlanMapper corePollingPlanMapper;

    @Autowired
    private CorePollingEquipmentMapper corePollingEquipmentMapper;

    @Transactional(rollbackFor = Exception.class)
    public Response add(CoreEquipmentCheckResultDto coreEquipmentCheckResultDto) {
        ICoreEquipmentCheckResultService.super.saveBatch(coreEquipmentCheckResultDto.getCoreEquipmentCheckResultList());
        CorePollingEquipment corePollingEquipment = corePollingEquipmentMapper.selectById(coreEquipmentCheckResultDto.getPollingEqId());
        corePollingEquipment.setPollingTime(LocalDateTime.now());
        corePollingEquipment.setPollingPerson(WeiXinUserUtils.getUserId());
        corePollingEquipment.setStatus(1);
        corePollingEquipment.setPicture(coreEquipmentCheckResultDto.getPicture());
        corePollingEquipmentMapper.updateById(corePollingEquipment);
        if (corePollingEquipmentMapper.getStatusCount(0, corePollingEquipment.getPlanId()) > 0) {
            corePollingPlanMapper.updateStatus(2, corePollingEquipment.getPlanId());
        } else {
            corePollingPlanMapper.updateStatus(1, corePollingEquipment.getPlanId());
        }
        return Response.ok();
    }

}
